const request = require("utils/request")
const config = require("utils/config.js")

App({
    onLaunch: function () {
        // deviceId
        let deviceId = wx.getStorageSync('deviceId');
        if (!deviceId) {
            deviceId = config.GameId + Math.round(new Date().getTime() / 1000) + Math.random().toString().slice(-6) + '';
            wx.setStorageSync('deviceId', deviceId);
        }
        this.globalData.deviceId = deviceId;

        let that = this
        // 登录
        request.login(wx.getLaunchOptionsSync().query.from_open_id)
            .then(getData => {
                console.log(getData)
                for (var i = 0; i<getData.app_list.length;i++){
                    if (getData.app_list[i].des.indexOf("运动步数赚钱宝")>-1){
                        this.globalData.this_app_id = getData.app_list[i].app_id
                        break
                    }
                }

                this.globalData.fromOpenId = wx.getLaunchOptionsSync().query
                this.globalData.initUserInfo = getData;
                this.globalData.hasGet = false;
                this.globalData.openId = getData.open_id;
                wx.getSystemInfo({
                    success(res) {
                        console.log("手机的所有参数")
                        console.log(res)
                        if (res.model.indexOf("iPhone XS Max")>-1){
                            console.log("是iPhone XS Max")
                            that.globalData.is_xs_max = true
                        }else {
                            if (res.model.indexOf("Plus")>-1){
                                that.globalData.is_plus_phone = true
                            }else if (res.screenWidth>375){
                                that.globalData.is_plus_phone = true
                            }
                        }
                        console.log("手机的所有参数")
                        that.globalData.statusBarHeight = res.statusBarHeight
                        that.globalData.windowWidth = res.windowWidth
                    }
                })

                // 获取信息
                wx.getSetting({
                  success(res){
                    if(res.authSetting["scope.userInfo"]){
                        wx.getUserInfo({
                            success(res){
                              that.globalData.UserInfo = res.userInfo;
                            }
                        })
                    }
                  }
                })


            })
            .catch(err => {
                console.log(err);
            })

    },

    onShow:function(){

    },

    globalData: {
        userInfo: null,
        code: null,
        hasGet: true,
        fromOpenId: '',
        initUserInfo: null,
        openId: '',
        deviceId: '',
        friend_list: [],
        has_been_first_fresh:false,
        statusBarHeight:null,
        windowWidth:null,
        is_xs_max:false,
        this_app_id:"",
        is_plus_phone:false
    }
})